#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMenuBar>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QGroupBox>
#include <QHBoxLayout>

// 在 MainWindow 构造函数中添加信号监控
#include <QSignalSpy>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置窗口标题和大小
    setWindowTitle("浊度计数据采集系统");
    setMinimumSize(600, 400);

    // 创建中央部件和布局
    centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    auto* mainLayout = new QVBoxLayout(centralWidget);

    // 默认显示欢迎信息
    auto* defaultLabel = new QLabel("请点击上方菜单选择操作", this);
    defaultLabel->setAlignment(Qt::AlignCenter);
    defaultLabel->setStyleSheet("font-size: 16px; color: #666;");
    mainLayout->addWidget(defaultLabel);

    // 创建菜单栏
    QMenuBar *menuBar = new QMenuBar(this);
    setMenuBar(menuBar);

    // 创建四个可点击菜单
    ClickableMenu *menuComm = new ClickableMenu("通讯", this);
    ClickableMenu *menuSet = new ClickableMenu("设置", this);
    ClickableMenu *menuTool = new ClickableMenu("工具", this);
    ClickableMenu *menuHelp = new ClickableMenu("帮助", this);

    // 添加菜单到菜单栏
    menuBar->addMenu(menuComm);
    menuBar->addMenu(menuSet);
    menuBar->addMenu(menuTool);
    menuBar->addMenu(menuHelp);

    // 监控 menuClicked 信号
    QSignalSpy spy(menuComm, &ClickableMenu::menuClicked);

    // 连接菜单点击信号
    connect(menuComm, &ClickableMenu::menuClicked, this, &MainWindow::onCommMenuClicked);
    connect(menuSet, &ClickableMenu::menuClicked, this, &MainWindow::onSetMenuClicked);
    connect(menuTool, &ClickableMenu::menuClicked, this, &MainWindow::onToolMenuClicked);
    connect(menuHelp, &ClickableMenu::menuClicked, this, &MainWindow::onHelpMenuClicked);

    // 打印信号监控器状态
    qDebug() << "[信号] 监控器是否有效：" << spy.isValid();
}

MainWindow::~MainWindow()
{
    delete ui;
}
// 通讯菜单点击处理
void MainWindow::onCommMenuClicked()
{
    qDebug() << "通讯菜单被点击了！";
    // 清除中央部件的现有内容
    QLayout* layout = centralWidget->layout();
    QLayoutItem* item;
    while ((item = layout->takeAt(0)) != nullptr) {
        QWidget* widget = item->widget();
        if (widget) widget->deleteLater();
        delete item;
    }

    // 添加通讯相关内容
    layout->addWidget(createCommWidget());
}

// 设置菜单点击处理
void MainWindow::onSetMenuClicked()
{
    // 清除中央部件的现有内容
    QLayout* layout = centralWidget->layout();
    QLayoutItem* item;
    while ((item = layout->takeAt(0)) != nullptr) {
        QWidget* widget = item->widget();
        if (widget) widget->deleteLater();
        delete item;
    }

    // 添加设置相关内容
    layout->addWidget(createSetWidget());
}

// 工具菜单点击处理
void MainWindow::onToolMenuClicked()
{
    // 清除中央部件的现有内容
    QLayout* layout = centralWidget->layout();
    QLayoutItem* item;
    while ((item = layout->takeAt(0)) != nullptr) {
        QWidget* widget = item->widget();
        if (widget) widget->deleteLater();
        delete item;
    }

    // 添加工具相关内容
    layout->addWidget(createToolWidget());
}

// 帮助菜单点击处理
void MainWindow::onHelpMenuClicked()
{
    // 清除中央部件的现有内容
    QLayout* layout = centralWidget->layout();
    QLayoutItem* item;
    while ((item = layout->takeAt(0)) != nullptr) {
        QWidget* widget = item->widget();
        if (widget) widget->deleteLater();
        delete item;
    }

    // 添加帮助相关内容
    layout->addWidget(createHelpWidget());
}

// 创建通讯菜单对应的内容界面（假设是串口设置界面）
QWidget* MainWindow::createCommWidget()
{
    QWidget* widget = new QWidget();
    auto* mainLayout = new QVBoxLayout(widget);

    // 添加标题
    auto* titleLabel = new QLabel("通讯设置", widget);
    titleLabel->setStyleSheet("font-size: 18px; font-weight: bold; margin-bottom: 15px;");
    mainLayout->addWidget(titleLabel);

    // 创建设置区域
    auto* groupBox = new QGroupBox("串口参数设置", widget);
    auto* formLayout = new QFormLayout(groupBox);

    // 串口号选择
    auto* portCombo = new QComboBox(groupBox);
    portCombo->addItems({"COM1", "COM2", "COM3", "COM4", "COM5"});
    formLayout->addRow("串口号:", portCombo);

    // 波特率选择
    auto* baudCombo = new QComboBox(groupBox);
    baudCombo->addItems({"9600", "19200", "38400", "57600", "115200"});
    baudCombo->setCurrentText("115200");
    formLayout->addRow("波特率:", baudCombo);

    // 数据位选择
    auto* dataBitsCombo = new QComboBox(groupBox);
    dataBitsCombo->addItems({"5", "6", "7", "8"});
    dataBitsCombo->setCurrentText("8");
    formLayout->addRow("数据位:", dataBitsCombo);

    // 校验位选择
    auto* parityCombo = new QComboBox(groupBox);
    parityCombo->addItems({"无", "奇校验", "偶校验"});
    formLayout->addRow("校验位:", parityCombo);

    // 停止位选择
    auto* stopBitsCombo = new QComboBox(groupBox);
    stopBitsCombo->addItems({"1", "1.5", "2"});
    formLayout->addRow("停止位:", stopBitsCombo);

    groupBox->setLayout(formLayout);
    mainLayout->addWidget(groupBox);

    // 添加按钮区域
    auto* buttonLayout = new QHBoxLayout();

    auto* openBtn = new QPushButton("打开串口", widget);
    connect(openBtn, &QPushButton::clicked, [=]() {
        QMessageBox::information(this, "提示", "串口已打开");
    });

    auto* closeBtn = new QPushButton("关闭串口", widget);
    connect(closeBtn, &QPushButton::clicked, [=]() {
        QMessageBox::information(this, "提示", "串口已关闭");
    });

    buttonLayout->addWidget(openBtn);
    buttonLayout->addWidget(closeBtn);
    buttonLayout->addStretch(); // 拉伸项，将按钮推到左侧

    mainLayout->addLayout(buttonLayout);
    mainLayout->addStretch(); // 拉伸项，使内容靠上显示

    return widget;
}

// 创建设置菜单对应的内容界面
QWidget* MainWindow::createSetWidget()
{
    QWidget* widget = new QWidget();
    auto* layout = new QVBoxLayout(widget);

    auto* label = new QLabel("程序设置", widget);
    label->setStyleSheet("font-size: 18px; font-weight: bold; margin-bottom: 15px;");
    layout->addWidget(label);

    auto* infoLabel = new QLabel("这里是程序的各种设置选项", widget);
    layout->addWidget(infoLabel);

    // 可以添加更多设置相关的控件

    layout->addStretch();
    return widget;
}

// 创建工具菜单对应的内容界面
QWidget* MainWindow::createToolWidget()
{
    QWidget* widget = new QWidget();
    auto* layout = new QVBoxLayout(widget);

    auto* label = new QLabel("工具", widget);
    label->setStyleSheet("font-size: 18px; font-weight: bold; margin-bottom: 15px;");
    layout->addWidget(label);

    auto* infoLabel = new QLabel("这里是各种工具的功能入口", widget);
    layout->addWidget(infoLabel);

    // 可以添加更多工具相关的控件

    layout->addStretch();
    return widget;
}

// 创建帮助菜单对应的内容界面
QWidget* MainWindow::createHelpWidget()
{
    QWidget* widget = new QWidget();
    auto* layout = new QVBoxLayout(widget);

    auto* label = new QLabel("帮助", widget);
    label->setStyleSheet("font-size: 18px; font-weight: bold; margin-bottom: 15px;");
    layout->addWidget(label);

    auto* infoLabel = new QLabel("这里是程序的帮助信息和使用说明", widget);
    layout->addWidget(infoLabel);

    // 可以添加更多帮助相关的控件

    layout->addStretch();
    return widget;
}
